﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;

namespace QuanLyBanHang
{
    public partial class F820_Tra_cuu_danh_muc_nha_khach_hang : Form
    {
        public F820_Tra_cuu_danh_muc_nha_khach_hang()
        {
            InitializeComponent();
            set_define_events();
        }

        #region Members
        BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
        #endregion

        #region Private Methods

        private void set_define_events()
        {
            this.Load += F820_Tra_cuu_danh_muc_nha_khach_hang_Load;
            this.m_grv.Click += m_grv_Click;
            this.m_txt_loc_du_lieu.TextChanged += m_txt_loc_du_lieu_TextChanged;
        }

        private void load_data_to_grid()
        {
            var v_lst_dm_khach_hang = db.DM_KHACH_HANG
                .Where(x => x.MA_KHACH_HANG.Contains(m_txt_loc_du_lieu.Text)
                || x.TEN_KHACH_HANG.Contains(m_txt_loc_du_lieu.Text)
                || m_txt_loc_du_lieu.Text.Equals(""))
                .Select(x => new
                {
                    ID = x.ID,
                    MA_KHACH_HANG = x.MA_KHACH_HANG,
                    TEN_KHACH_HANG = x.TEN_KHACH_HANG,
                    DIA_CHI = x.DIA_CHI,
                    DIEN_THOAI = x.DIEN_THOAI
                })
                .OrderBy(x => x.MA_KHACH_HANG)
                .ToList();

            m_grv.DataSource = v_lst_dm_khach_hang;
            m_grv.Columns[0].HeaderText = "ID";
            m_grv.Columns[0].Visible = false;
            m_grv.Columns[1].HeaderText = "Mã khách hàng";
            m_grv.Columns[1].Width = 80;
            m_grv.Columns[2].HeaderText = "Tên khách hàng";
            m_grv.Columns[2].Width = 140;
            m_grv.Columns[3].HeaderText = "Địa chỉ";
            m_grv.Columns[3].Width = 200;
            m_grv.Columns[4].HeaderText = "Điện thoại";
            m_grv.Columns[4].Width = 100;
            m_grv.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
        #endregion

        #region Events
        private void F820_Tra_cuu_danh_muc_nha_khach_hang_Load(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        void m_txt_loc_du_lieu_TextChanged(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        void m_grv_Click(object sender, EventArgs e)
        {
            try
            {
                decimal v_dc_id_kh = (decimal)m_grv.CurrentRow.Cells[0].Value;
                var v_dm_khach_hang = db.DM_KHACH_HANG.FirstOrDefault(x => x.ID == v_dc_id_kh);

                if (v_dm_khach_hang != null)
                {
                    m_txt_ma_kh.Text = v_dm_khach_hang.MA_KHACH_HANG;
                    m_txt_ten_kh.Text = v_dm_khach_hang.TEN_KHACH_HANG;
                    m_txt_dia_chi_kh.Text = v_dm_khach_hang.DIA_CHI;
                    m_txt_dien_thoai.Text = v_dm_khach_hang.DIEN_THOAI.ToString();
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_tim_kiem_khach_hang_Click(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_quay_lai_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        #endregion
    }
}
